package butines.kraz.state;

import butines.core.game.Message;
import butines.kraz.entity.AbstractState;
import butines.kraz.entity.MessageType;
import butines.kraz.entity.Unit;

public class UnitMoveState extends AbstractState<Unit> {
	
	private static final UnitMoveState instance = new UnitMoveState();
	
	private float slowingDistance = 32f; 
	
	public static UnitMoveState getInstance() {
		return instance;
	}

	@Override
	public void enter(Unit unit) {
		
		unit.getSteering().getArrivel().setSlowingDistance(slowingDistance);
		unit.getSteering().getArrivel().on(unit.getTarget());
		unit.getSteering().getAvoidance().on();
		
		unit.getPathPlanning().calcule();

		if (unit.getSprite().isPlaying("walk")) {
			unit.getSprite().resume();
		} else {
			unit.getSprite().play("walk");
		}
		
	}

	@Override
	public void leave(Unit unit) {
		unit.getSteering().getArrivel().off();
		unit.getSteering().getAvoidance().off();
	}
	
	@Override
	public void execute(Unit owner) {
		
		if (owner.atTarget() || owner.getPathPlanning().isTimeout()) {
			owner.stay();
		}
		
	}

	@Override
	public boolean onMessage(Message message) {
		
		final MessageType type = message.getType();
		
		switch (type) {
			
			case COLLIDED_WITH: {
				
//				Unit other = message.getSender();
//				
//				if (!other.inState(UnitStayState.class)) {
//					if (other.getPosition().distance(owner.getTarget()) < other.getRadius()) {
//						owner.stay();
//					}
//				}
				
				System.out.println(message.getReceiver().getId() + " collided with " + message.getSender().getId());
				return true;
			}
			
		}
		
		return false;
	}
	
	
	
}
